package com.study.java.jinjie.file.lianxi;


import java.io.*;
import java.nio.FloatBuffer;
import java.util.Set;

public class Scoreset {

    public Scoreset(Set<Student> studentSet) {
        this.studentSet = studentSet;
    }

    private Set<Student> studentSet ;

    public void pingJun3() {
//        1.遍历SET获取Student的学号,姓名,成绩
//        Iterator<Student> integer = studentSet.iterator();
//        while (integer.hasNext()){
//            Student student = integer.next();
//        }
        int i= 0;
        for (Student student : studentSet) {
            i++;
            long num =student.getNum();
            String name1 =student.getName();
            int chengji =student.getChengJi();
            xieRuTxt2(i,num, name1, chengji );
        }

    }
//        2.创建student.txt
//        3.获取的学生信息写入Stedent.txt
    public void xieRuTxt(int i,long num,String name1,int chengji ){
        String dir = "h:\\testDir\\student";
        File file = new File(dir);

        try {
                file.mkdir();
                FileWriter writer  = new FileWriter(dir + "\\studentSet.txt", true);

                writer.write("序号\t" + "     学号\t" + "  姓名\t" + "    成绩\n");
                writer.write("------------------------------\n");

                writer.write(i + "     \t" + num + "    \t" + name1 + "    \t" + chengji + "\n");
                writer.write("------------------------------\n");
                writer.close();
                System.out.println("文件已存在");
                System.out.println("创建成功!");
    } catch (IOException e) {
            System.out.println("An error occurred.异常");
            e.printStackTrace();
        }

    }

    public void xieRuTxt2(int i,long num,String name1,int chengji ){
        String dir = "h:\\testDir\\student";
        File file = new File(dir);

        try {
            file.mkdir();
            BufferedWriter writer  = new BufferedWriter (new FileWriter(dir + "\\studentSet.txt",true));
            writer.write("序号\t" + "     学号\t" + "  姓名\t" + "    成绩\n");
            writer.write("------------------------------\n");

            writer.write(i + "     \t" + num + "    \t" + name1 + "    \t" + chengji + "\n");
            writer.write("------------------------------\n");
            writer.close();
            System.out.println("创建成功!");
        } catch (IOException e) {
            System.out.println("An error occurred.异常");
            e.printStackTrace();
        }

    }
    public void write(String content){
        FileOutputStream fos = null;
        try {
            byte[] bytes = content.getBytes();  // 将字符串转为字节数组
            fos = new FileOutputStream("d:\\testDir\\student\\test22.txt");  // 获取文件对象
            fos.write(bytes); // 写入字节数组
            System.out.println("文件写入完成！");
        } catch (IOException e) {
            System.out.println("文件写入失败：" + e.getMessage());
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }


    public void getStudent() {

        String context = "序号\t     学号\t   姓名\t   成绩\n";
        context += "------------------------------\n";

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("序号\t     学号\t   姓名\t   成绩\n");

        int i= 1;
        for (Student student : studentSet) {

            long num =student.getNum();
            String name1 =student.getName();
            int chengji =student.getChengJi();
            xieRuTxt(i,num, name1, chengji );

            context += i+"\t"+student.getNum()+"\t"+student.getName()+"\t"+student.getChengJi()+"\n";
            context += "------------------------------\n";
            i++;
        }

        write(context);
        System.out.println(context);
    }
}
